
--22.给【威尔逊】增加沃拓克斯的特性：打开地图后可右键点击传送到任意地点，不能传到战争迷雾、海洋、船上（为了防止溺水），传送动画使用沃拓克斯的即可，每次消耗30理智值，理智值为0时不能传送，不会显示动作。
local function CanBlinkTo(pt)
    return false--[[TheWorld.Map:IsPassableAtPoint(pt:Get()) and not TheWorld.Map:IsGroundTargetBlocked(pt) -- NOTES(JBK): Keep in sync with blinkstaff. [BATELE]--]]
end

local function CanBlinkFromWithMap(pt)
    return true -- NOTES(JBK): Change this if there is a reason to anchor Wortox when trying to use the map to teleport.
end

local function ReticuleTargetFn(inst)
    return ControllerReticle_Blink_GetPosition(inst, inst.CanBlinkTo)
end

local function CanSoulhop(inst, souls)
	if inst.replica.sanity:GetCurrent() >= TUNING.WILSON_SANITYVALUE_HOP_ATLEAST then
        local rider = inst.replica.rider
        if rider == nil or not rider:IsRiding() then
            return true
        end
    end
    return false
end

local function GetPointSpecialActions(inst, pos, useitem, right)
    if right and useitem == nil then
        local canblink
        if inst.checkingmapactions then
            canblink = inst.CanBlinkFromWithMap(inst:GetPosition())
        else
            canblink = inst.CanBlinkTo(pos)
        end
        if canblink and inst.CanSoulhop and inst:CanSoulhop() then
            return { ACTIONS.BLINK }
        end
    end
    return {}
end

local function OnSetOwner(inst)
    if inst.components.playeractionpicker ~= nil then
        inst.components.playeractionpicker.pointspecialactionsfn = GetPointSpecialActions
    end
end

local function TryToPortalHop(inst, souls, consumeall)
	if inst.components.sanity.current >= TUNING.WILSON_SANITYVALUE_HOP_ATLEAST then
		return true
	end
end

local function OnSoulHop(inst)
	inst.components.sanity:DoDelta(-TUNING.WILSON_SANITYVALUE_HOP_ATLEAST)
end

AddPrefabPostInit("wilson", function(inst)
	inst.CanSoulhop = CanSoulhop
    inst.CanBlinkTo = CanBlinkTo
    inst.CanBlinkFromWithMap = CanBlinkFromWithMap
    inst:ListenForEvent("setowner", OnSetOwner)
	
	inst:AddComponent("reticule")
	inst.components.reticule.targetfn = ReticuleTargetFn
	inst.components.reticule.ease = true
	
	if not TheWorld.ismastersim then return end
	
	inst.TryToPortalHop = TryToPortalHop
	inst:ListenForEvent("soulhop", OnSoulHop)
end)

------------------ 主机 ---------------------------
AddStategraphPostInit("wilson", function(sg)
    local old_BLINK_MAP = sg.actionhandlers[ACTIONS.BLINK_MAP].deststate
    sg.actionhandlers[ACTIONS.BLINK_MAP].deststate = function(inst, action,...)
        if inst:HasTag("wilson") and inst.components.sanity.current >= TUNING.WILSON_SANITYVALUE_HOP_ATLEAST then 
            return "portal_jumpin_pre"
        end
        return old_BLINK_MAP(inst, action,...)
    end
end)
------------------ 客机 ---------------------------
AddStategraphPostInit("wilson_client", function(sg)
    local old_BLINK_MAP = sg.actionhandlers[ACTIONS.BLINK_MAP].deststate
    sg.actionhandlers[ACTIONS.BLINK_MAP].deststate = function(inst, action,...)
        if inst:HasTag("wilson") and inst.replica.sanity:GetCurrent() >= TUNING.WILSON_SANITYVALUE_HOP_ATLEAST then 
            return "portal_jumpin_pre"
        end
        return old_BLINK_MAP(inst, action,...)
    end
end)

local old_BLINK_MAP_stroverridefn = ACTIONS.BLINK_MAP.stroverridefn
ACTIONS.BLINK_MAP.stroverridefn = function(act, ...)
	if act.doer ~= nil and act.doer:HasTag("wilson") then
		return "跃迁"
	end
	return old_BLINK_MAP_stroverridefn(act, ...)
end

AddClassPostConstruct("screens/mapscreen", function(self)
    local old_ProcessRMBDecorations = self.ProcessRMBDecorations
    function self:ProcessRMBDecorations(...)
        if old_ProcessRMBDecorations then
            old_ProcessRMBDecorations(self, ...)
        end
        if self.owner:HasTag("wilson") and self.decorationdata and self.decorationdata.rmbents then
            if self.decorationdata.rmbents[1] then
                self.decorationdata.rmbents[1]:Hide()
            end
            if self.decorationdata.rmbents[2] then
                self.decorationdata.rmbents[2]:Hide()
            end
            if self.decorationdata.rmbents[3] then
                self.decorationdata.rmbents[3]:Hide()
            end
        end
    end
end)